const express = require('express');
const usersData =  require('../db/users.json');
const loginVirefy = require('../middleware/loginVirefy');

const router = express.Router();

// 请求接口 - 登录
router.post('/login',(req,res)=>{
    // 获取请求体数据
    // console.log(req.body);// {username:xiaowang,pwd:123456}
    let {username,pwd} = req.body;

    // 验证 账号 和 密码是否一致
    let result = usersData.find(item=>item.username===username&&item.pwd===pwd);

    if(result){
        // res.send('登录成功');
        req.session.username = username;
        res.render('loginSuccess.html');
    }else{
        res.send('用户名或密码错误');
    }
})

//请求接口 - 退出登录
router.get('/logout',loginVirefy,(req,res)=>{
    req.session.destroy(()=>{
        res.render('logoutSuccess.html');
    })
});
module.exports = router;